package com.opal.jx.service.impl;

import com.opal.jx.entity.table.JxPost;
import com.opal.jx.entity.table.JxPostExample;
import com.opal.jx.mapper.mysql.JxPostMapper;
import com.opal.jx.service.JxPostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JxPostServiceImpl implements JxPostService {

    @Autowired
    private JxPostMapper jxPostMapper;

    @Override
    public JxPost getJxPostById(Long id) {
        JxPost jxPost = jxPostMapper.selectByPrimaryKey(id);
        return jxPost;
    }

    @Override
    public List<JxPost> getJxPostByIds(List<Long> ids) {
        JxPostExample jxPostExample = new JxPostExample();
        jxPostExample.createCriteria().andIdIn(ids);

        List<JxPost> jxPosts = jxPostMapper.selectByExample(jxPostExample);
        return jxPosts;
    }

    @Override
    public List<JxPost> getJxPostBycondition(JxPost jxPostCondition) {
        JxPostExample jxPostExample = this.getJxPostExample(jxPostCondition);
        List<JxPost> jxPosts = jxPostMapper.selectByExample(jxPostExample);
        return jxPosts;
    }

    /**
     * 根据JxPost来设置查询条件：获取JxPostExample条件对象
     * @param jxPostCondition jxPost对象
     * @return
     */
    private JxPostExample getJxPostExample(JxPost jxPostCondition){
        JxPostExample jxPostExample =new JxPostExample();
        JxPostExample.Criteria criteria = jxPostExample.createCriteria();
        Long id = jxPostCondition.getId();
        String pkDept = jxPostCondition.getPkDept();
        String name = jxPostCondition.getName();
        Long pid=jxPostCondition.getPid();
        if (id !=null){
            criteria.andIdEqualTo(id);
        }
        if (pid !=null){
            criteria.andPidEqualTo(pid);
        }
        if (!StringUtils.isBlank(pkDept)){
            criteria.andPkDeptEqualTo(pkDept);
        }
        if (!StringUtils.isBlank(name)){
            criteria.andNameEqualTo(name);
        }
        return jxPostExample;
    }
}
